package com.foreknow.sercive.Impl;

import com.foreknow.dao.Impl.BusinessDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Business;
import com.foreknow.sercive.BusinessService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BusinessServiceImpl implements BusinessService {
    BusinessDaoImpl dao=new BusinessDaoImpl();
    @Override
    public boolean add(Business business) {
        DBManager dbmanager = DBManager.getInstance();
        Connection conn = dbmanager.getConnection();
        boolean isRight = false;
        try {
            isRight = dao.insert(business);
            dao.setConnection(conn);
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    @Override
    public List<Object> selectAll() {
        DBManager dbmanager = DBManager.getInstance();
        Connection conn = dbmanager.getConnection();
        List<Object> list=null;
        try {
            list = dao.queryall();
            dao.setConnection(conn);
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Object> selectBuName(String name) {
        DBManager dbmanager = DBManager.getInstance();
        Connection conn = dbmanager.getConnection();
        List<Object> list=null;
        try {
            list = dao.qaueryByName(name);
            dao.setConnection(conn);
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean deleteById(int id) {
        DBManager dbmanager = DBManager.getInstance();
        Connection conn = dbmanager.getConnection();
        boolean isRight = false;
        try {
            isRight = dao.deleteById(id);
            dao.setConnection(conn);
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    @Override
    public boolean updateById(Business business, int id) {
        DBManager dbmanager = DBManager.getInstance();
        Connection conn = dbmanager.getConnection();
        boolean isRight = false;
        try {
            isRight = dao.updateById(business,id);
            dao.setConnection(conn);
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }
}
